var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    //关闭
    var closed = $(".close");
    closed.click(function() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //监听提交
    form.on('submit(submit)', function(){
        //提交按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        addOrUpdateCompany();
    });
});

/**
 * 新增/修改
 */
function addOrUpdateCompany() {
    var comId = $.trim($("#comId").val());
    // 公司名称
    var comName = $.trim($("#comName").val());
    // 地址
    var address = $.trim($("#address").val());
    //联系人
    var contacts = $.trim($("#contacts").val());
    // 手机
    var cellphone = $.trim($("#cellphone").val());
    // 电话
    var telephone = $.trim($("#telephone").val());
    // 传真
    var fax = $.trim($("#fax").val());
    // 邮箱
    var email = $.trim($("#email").val());
    // 备注
    var remark = $("#remark").val();

    var type = $("#type").val();
    if (null == type || '' == type) {
        type = 'add';
    }
    var paramData = {
        "company.id" : comId,
        "company.name" : comName,
        "company.address" : address,
        "company.contacts" : contacts,
        "company.cellphone" : cellphone,
        "company.telephone" : telephone,
        "company.fax" : fax,
        "company.email" : email,
        "company.remark" : remark,
        "type" : type
    };
    $.ajax({
        type : 'post',
        url : basePath + "company/saveCompany.action",
        data : paramData,
        dataType : 'text',
        success : function(data) {
            if (data != null && data != "" && data == "success") {
                closeAndRefresh();
            } else if (data == "exist") {
                layer.open({
                    title: '提示',
                    icon: 0,
                    content: '公司已存在!'
                });
            } else {
                var content = '';
                if (type == "add") {
                    content='新增失败!';
                } else {
                    content='修改失败!';
                }
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: content,
                    yes:function () {
                        closeAndRefresh();
                    }
                });
            }
            //提交按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });
}